package io.kimmking.dubbo.demo.provider.provider;

import io.kimmking.dubbo.demo.api.AccountProvider;
import io.kimmking.dubbo.demo.api.request.ExchangeRequest;
import io.kimmking.dubbo.demo.provider.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.hmily.annotation.HmilyTCC;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author gechenpeng
 * @date 2021-06-07
 */
@DubboService(version = "1.0.0")
@Slf4j
public class AccountProviderImpl implements AccountProvider {

    private final AccountService accountService;

    @Autowired(required = false)
    public AccountProviderImpl(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    @HmilyTCC(confirmMethod = "confirmExchange", cancelMethod = "cancelExchange")
    public void exchangeForRmb(ExchangeRequest exchangeRequest) {
        log.info("b start to try");
        accountService.freezeRmbAmount(exchangeRequest.getUserId(), exchangeRequest.getRmbAmount());
    }

    public void confirmExchange(ExchangeRequest exchangeRequest) {
        log.info("b start to confirm");
        accountService.doExchangeForDollar(exchangeRequest.getUserId(), exchangeRequest.getRmbAmount());
    }

    public void cancelExchange(ExchangeRequest exchangeRequest) {
        log.info("b start to cancel");
        accountService.unfreezeRmbAmount(exchangeRequest.getUserId(), exchangeRequest.getRmbAmount());
    }
}
